const path = require('path');
const ExtarctTextwebpackPlugin = require('extract-text-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const webpack = require('webpack');
const CleanWebpackPlugin = require('clean-webpack-plugin');

module.exports = {
    entry: "./src/js/index.js",
    output: {
        filename: "js/index.js",
        path: path.resolve(__dirname, './dist')
    },
    module: {
        rules: [
            //js
            {
                test: /\.js$/,
                use: {
                    loader: "babel-loader",
                    options: {
                        presets: ["@babel/preset-env"]
                    }
                }
            },
            //scss 
            {
                test: /\.scss$/,
                use: ExtarctTextwebpackPlugin.extract({
                    fallback: 'style-loader',
                    use: ['css-loader', 'sass-loader']
                })
            },
            //img
            {
                test: /\.(jpg|gif)$/,
                use: {
                    loader: "url-loader",
                    options: {
                        name: "img/[name].[ext]",
                        limit: 2500,
                        pbulicPath: "/"
                    }
                }
            },
            //html
            {
                test: /\.html$/,
                use: {
                    loader: 'html-loader',
                    options: {
                        filename: "index.html"
                    }
                }
            }
        ]
    },
    plugins: [
        new CleanWebpackPlugin(),
        new ExtarctTextwebpackPlugin({
            filename: 'style/index.css'
        }),
        new HtmlWebpackPlugin({
            template: "./src/index.html"
        }),
        new webpack.HotModuleReplacementPlugin()
    ],
    devServer: {
        port: 3500,
        host: "localhost",
        open: true,
        hot: true,
    },
    resolve: {
        alias: {
            'css': path.resolve(__dirname, "./src/style/index.scss")
        }
    }
}